package dao;

import java.sql.*;

import entity.User;

public class UserDAO {
	public boolean zhuce(User u) throws Exception{
		Connection con = DBUtil.getConn();
		
		String sql = "select Uname,Upassword,Uemail,UphoneNumber from users";
		PreparedStatement pstmt = con.prepareStatement(sql);
		ResultSet rs = pstmt.executeQuery();
		String name;

		while(rs.next()){
			name = rs.getString(1);
			if(name.equals(u.getuName())){
				DBUtil.close(con);
				return false;
			}
		}
		String sql2 = "insert into users"
				+ " values(?,?,?,?)";
		PreparedStatement pstmt2 = con.prepareStatement(sql2);
		pstmt2.setString(1, u.getuName());
		pstmt2.setString(2, u.getuPassword());
		pstmt2.setString(3, u.getEmail());
		pstmt2.setString(4, u.getNumber());
		pstmt2.executeUpdate();
		DBUtil.close(con);
		return true;
	}
	public boolean login(User u) throws SQLException{
		Connection con = DBUtil.getConn();
		String sql = "select Uname,Upassword from users";
		PreparedStatement pstmt = con.prepareStatement(sql);
		ResultSet rs = pstmt.executeQuery();
		String name;
		String password;
		while(rs.next()){
			name = rs.getString(1);
			password = rs.getString(2);
			if(name.equals(u.getuName())&&password.equals(u.getuPassword())){
				try {
					DBUtil.close(con);
				} catch (Exception e) {
					e.printStackTrace();
				}
				return true;
			}
		}
		try {
			DBUtil.close(con);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	
}
